package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeviceDataServiceImpl implements DeviceDataService {
    @Autowired
    DeviceDataMapper deviceDataMapper;
    @Override
    public PageResponse<DeviceDataVo> page(DeviceDataPageReqDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());

        LocalDateTime start= LocalDateTimeUtil.of(dto.getStartTime());
        LocalDateTime end=LocalDateTimeUtil.of(dto.getEndTime());
        dto.setStartDate(start);
        dto.setEndDate(end);

        Page<DeviceDataVo> page=deviceDataMapper.page(dto);

        return PageResponse.of(page,DeviceDataVo.class);
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        LocalDateTime begin = LocalDateTimeUtil.of(startTime);
        LocalDateTime end = LocalDateTimeUtil.of(endTime);

        List<DeviceDataGraphVo> deviceDataGraphVoList= deviceDataMapper.queryDeviceDataListByDay(iotId, functionId,begin, end);
        Map<String, Double> collect = deviceDataGraphVoList.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));

        List<DeviceDataGraphVo> deviceDataGraphVos = DeviceDataGraphVo.dayInstance(begin);
        deviceDataGraphVos.forEach(deviceDataGraphVo -> {
            if(ObjectUtil.isNotEmpty(collect.get(deviceDataGraphVo.getDateTime()))){
                deviceDataGraphVo.setDataValue(collect.get(deviceDataGraphVo.getDateTime()));
            }
        });
        return deviceDataGraphVos;
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        LocalDateTime begin = LocalDateTimeUtil.of(startTime);
        LocalDateTime end = LocalDateTimeUtil.of(endTime);

        List<DeviceDataGraphVo> deviceDataGraphVoList= deviceDataMapper.queryDeviceDataListByWeek(iotId, functionId,begin, end);
        Map<String, Double> collect = deviceDataGraphVoList.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));

        List<DeviceDataGraphVo> deviceDataGraphVos = DeviceDataGraphVo.weekInstance(begin);
        deviceDataGraphVos.forEach(deviceDataGraphVo -> {
            if(ObjectUtil.isNotEmpty(collect.get(deviceDataGraphVo.getDateTime()))){
                deviceDataGraphVo.setDataValue(collect.get(deviceDataGraphVo.getDateTime()));
            }
        });
        return deviceDataGraphVos;

    }
}
